package hluck.me.srorage.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hluck.me.srorage.sys.common.DataGridView;
import hluck.me.srorage.sys.common.ResultObj;
import hluck.me.srorage.sys.common.WebUtils;
import hluck.me.srorage.sys.domain.SysNotice;
import hluck.me.srorage.sys.domain.SysUser;
import hluck.me.srorage.sys.service.SysNoticeService;
import hluck.me.srorage.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/sys/sys-notice")
public class SysNoticeController {

    @Autowired
    SysNoticeService sysNoticeService;

    /**
     * 查询
     */
    @RequestMapping("loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo noticeVo){
        Page<SysNotice> page = new Page<>(noticeVo.getPage(), noticeVo.getLimit());
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()),"title",noticeVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(noticeVo.getOpername()),"opername",noticeVo.getOpername());
        queryWrapper.ge(noticeVo.getStartTime()!=null,"createtime",noticeVo.getStartTime());
        queryWrapper.le(noticeVo.getEndTime()!=null,"createtime",noticeVo.getEndTime());
        queryWrapper.orderByDesc("createtime");

        this.sysNoticeService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加公告
     */
    @RequestMapping("addNotice")
    public ResultObj addNotice(NoticeVo noticeVo){
        try {
            SysUser user= (SysUser) WebUtils.getSession().getAttribute("user");
            noticeVo.setCreatetime(new Date());
            noticeVo.setOpername(user.getName());
            sysNoticeService.save(noticeVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 更新公告
     */
    @RequestMapping("updateNotice")
    public ResultObj updateNotice(NoticeVo noticeVo){
        try {
            sysNoticeService.updateById(noticeVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除公告
     */
    @RequestMapping("deleteNotice")
    public ResultObj deleteNotice(Integer id){
        try{
            this.sysNoticeService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @RequestMapping("batchDeleteNotice")
    public ResultObj batchDeleteNotice(NoticeVo noticeVo){
        try{
            ArrayList<Integer> list = new ArrayList<>();
            for (Integer id : noticeVo.getIds()) {
                list.add(id);
            }
            this.sysNoticeService.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

